package Servidor.Modelo.Games;

import BaseDatos.FachadaBBDD;
import Servidor.Modelo.Users.User;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Observable;

/**
 * @author Angel Lopez
 */
public class Game extends Observable {
    private BoardPlaying board;
    private GameRunning game_running;
    private int id_game;
    private String name_game;
    private int id_owner;
    private int id_winner;

    private String date_start;
    private String date_finish;

    private Player player_aux;
    private ArrayList<Player> list_player;
    private int active;
    
    
    public Game(){
       id_game = 0;
       name_game = "";
       id_owner = 1;
       id_winner = -1;
       board = new BoardPlaying(id_game);
       active = 1;
       list_player = new ArrayList<Player>();
    }
    

    
    public Game(int id_owner, String name_game){
        this();
        this.id_owner = id_owner;
        this.name_game = name_game;
        id_game = FachadaBBDD.get_instance().insert_game(id_owner, name_game);
        
        
    }
    
    public void start_game(){
        game_running = new GameRunning(this);
        
        if(!game_running.list_player_is_empty())
            game_running.iniciar_juego();
    }
    
    public void finish_game(){
        
        int error = FachadaBBDD.get_instance().update_game_winner(id_game, id_winner);
    }
    
    public void update_DDBB(){
        // TODO ACTUALIZAR DDBB
    }
    
    public BoardPlaying getBoardPlaying(){
        return board;
    }
    
    public void notify_change(BoardPlaying board){
        this.board = board;
        this.setChanged();
        this.notifyObservers(board);
    }
    

    
    public void roll_dice(int dice1, int dice2){
        game_running.roll_dice(dice1, dice2);
    }
    
    public int number_player(){
        return list_player.size();
    }
    
    public int number_player_active(){
        int number_player = 0;
        Iterator it = list_player.iterator();
            while(it.hasNext()){
                player_aux = (Player) it.next();
                if(player_aux.isActive())
                    number_player++;
            }
        return number_player;
    }
    
    /**
     * @param id_user
     * @return -1 en caso de error
     */
    public int add_player_id(int id_user){
        if(list_player.size()==8){
            // TODO // BORRAR
            System.out.println("ALCANZADO NUMERO MAXIMO JUGADORES");
            return -1;
        }
        else{
            player_aux = new Player(id_user);
            
            User user_aux = FachadaBBDD.get_instance().select_user(id_user);           
            player_aux.setNick(user_aux.getNick());
            
            FachadaBBDD.get_instance().insert_player(id_game, id_user);
            list_player.add(player_aux);
        }

        return list_player.size();  
    }
    
    public int remove_player_id(int id_user){
        if(list_player.isEmpty()){
            // TODO // BORRAR
            System.out.println("NO HAY USUARIOS QUE BORRAR");
            return -1;
        }
        else{
            Iterator it = list_player.iterator();
            while(it.hasNext()){
                player_aux = (Player) it.next();
                if(id_user == player_aux.getId_player())
                    player_aux.setActive(false);
            }
            
        }

        return 1;  
    }      
    
    public int get_player_id(int index){
        return list_player.get(index).getId_player();
    }
    
    public ArrayList<Player> get_list_payers(){
        return list_player;
    }


    public String getDate_finish() {
        return date_finish;
    }

    public String getDate_start() {
        return date_start;
    }

    public int getId_game() {
        return id_game;
    }

    public int getId_owner() {
        return id_owner;
    }

    public int getId_winner() {
        return id_winner;
    }

    public void setDate_finish(String date_finish) {
        this.date_finish = date_finish;
    }

    public void setDate_start(String date_start) {
        this.date_start = date_start;
    }

    public void setId_owner(int id_owner) {
        this.id_owner = id_owner;
    }

    public void setId_winner(int id_winner) {
        this.id_winner = id_winner;
    }

    public void setId_game(int id_game) {
        this.id_game = id_game;
        board.setId_game(id_game);
    }

    public int getActive() {
        return active;
    }

    public void setActive(int active) {
        this.active = active;
    }

    public String getName_game() {
        return name_game;
    }

    public void setName_game(String name_game) {
        this.name_game = name_game;
    }
    
    
    
    
    
    
    
}
